package com.xc.disk.web.rest;

import com.xc.disk.utils.VerifyCodeUtils;
import com.xc.disk.dto.VerifyCodeDto;
import com.xc.disk.utils.RedisUtils;
import com.xc.tool.utils.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 验证码前端控制器</p>
 * <P>创建人: 小程</p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/7/2</p>
 *
 * @version v1.0
 */
@RestController
public class VerifyCodeRest {

    /**
     * 获取验证码接口
     *
     * @return 验证码信息
     */
    @GetMapping("/verify/code")
    public VerifyCodeDto getVerifyCode() throws UnsupportedEncodingException {
        String code = UUID.randomUUID().toString();
        Base64Utils.encodeToString(code.getBytes("UTF-8"));
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        RedisUtils.set(code, verifyCode, 1800000l);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        VerifyCodeUtils.outputImage(200, 80, byteArrayOutputStream, verifyCode);
        VerifyCodeDto validateCodeDto = new VerifyCodeDto(code, byteArrayOutputStream.toByteArray());
        return validateCodeDto;
    }
}
